package servlet;

import entiny.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;

/**
 * Created by liujunnan on 2017/11/25.
 */
public class RegServlet extends HttpServlet {
    public RegServlet() {
        super();
    }

    @Override
    public void init() throws ServletException {
        super.init();
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        File directory = new File("");
        String a = directory.getAbsolutePath();
        try {

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            request.setCharacterEncoding("utf-8");
            User user = new User();
            user.setUsername(request.getParameter("username"));
            user.setMypassword(request.getParameter("mypassword"));
            user.setGender(request.getParameter("gender"));
            user.setEmail(request.getParameter("email"));
            user.setFavorites(request.getParameterValues("favorite"));
            user.setIntroduce(request.getParameter("introduce"));
            if (request.getParameterValues("isAccpet") != null) {
                user.setAccept(true);
            } else {
                user.setAccept(false);
            }
            user.setBirthday(sdf.parse(request.getParameter("birthday")));
            request.getSession().setAttribute("regUser", user);
            request.getSession().setAttribute("isAccept", user.isAccept());
            String context = request.getContextPath();
            //重定向获取上下文环境
            response.sendRedirect(request.getContextPath()+"/pages/userinfo.jsp");
            //斜线表示项目的根目录
            //request.getRequestDispatcher("/pages/userinfo.jsp").forward(request, response);
            //../后面是相对路径 2 什么都不加就是相对路径
            //request.getRequestDispatcher("../pages/userinfo.jsp").forward(request, response);
            //request.getRequestDispatcher("classpath:/pages/userinfo.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void destroy() {
        super.destroy();
    }

}
